package com.chaber.containers.presenters;

import java.util.List;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import com.chaber.containers.beans.ProductCategoryData;
import com.chaber.containers.beans.ProductData;
import com.chaber.containers.beans.searchcriteria.ProductSearchCriteria;
import com.chaber.containers.services.ProductCategoryService;
import com.chaber.containers.services.ProductService;
import com.chaber.containers.views.ProductListView;

public class ProductListPresenter extends AbstractPresenter {

	@Autowired
	private ProductListView productListView;

	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductCategoryService productCatService;

	@Autowired
	private ApplicationContext applicationContext;

	@Override
	public void bindViewActions() {
		this.productListView.setOnSelectionChanged(new EventHandler<Event>() {
			@Override
			public void handle(Event arg0) {
				if (productListView.isSelected()) {
					initAvailableCategories();
				}
			}
		});
		
		this.productListView.getSearchButton().setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent evt) {
				searchProducts();
			}
		});

		this.productListView.getClearButton().setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent evt) {
				clearProductSearchCriteria();
			}
		});
	}
	
	@Override
	public void postConstruct() {
		super.postConstruct();
		this.initAvailableCategories();
	}
	
	private void initAvailableCategories() {
		this.productListView.getCatField().getItems().clear();
		List<ProductCategoryData> categories = this.productCatService.findAll();
		this.productListView.getCatField().getItems().addAll(categories);
	}
	
	private void searchProducts() {
		ProductSearchCriteria productSearchCriteria = this.populateSearchCriteriaFromView();
		List<ProductData> products = productService.findAll(productSearchCriteria);

		if (this.productListView.getProductsTableItems() != null) {
			this.productListView.getProductsTableItems().clear();
			this.productListView.getProductsTableItems().addAll(products);
		}
	}

	private void clearProductSearchCriteria() {
		if (this.productListView.getProductsTableItems() != null) {
			this.productListView.getProductsTableItems().clear();
		}

		this.productListView.clearCriteria();
	}

	private ProductSearchCriteria populateSearchCriteriaFromView() {
		ProductSearchCriteria searchCriteria = this.applicationContext.getBean(ProductSearchCriteria.class);

		searchCriteria.setBarCode(this.productListView.getBrField().getText());
		searchCriteria.setName(this.productListView.getNameField().getText());
		
		if (!StringUtils.isEmpty(this.productListView.getAmountField().getText())) {
			searchCriteria.setAmount(Integer.valueOf(this.productListView.getAmountField().getText()));
		}
		
		searchCriteria.setCdFrom(this.productListView.getCdFromField().getValue());
		searchCriteria.setCdTo(this.productListView.getCdToField().getValue());
		searchCriteria.setMdFrom(this.productListView.getMdFromField().getValue());
		searchCriteria.setMdTo(this.productListView.getMdToField().getValue());
		
		if (this.productListView.getCatField().getValue() != null) {
			searchCriteria.setCategoryId(this.productListView.getCatField().getValue().getId());
		}

		return searchCriteria;
	}
}
